package com.moon.game.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.moon.game.manager.FeteMG;
import com.moon.game.manager.ItemMG;
import com.moon.game.resource.model.Fete;
import com.moon.game.resource.model.Item;
import com.moon.game.util.Utils;


/**
 * @description 祭祀服务
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-8-6 上午11:17:08
 */
public class FeteService {
	public static int COMMON_INTERVAL_LEVEL = 10;//祭祀获取非道具间隔等级
	public static int ITEM_INTERVAL_LEVEL = 5;//祭祀获取道具间隔等级
	public static int PRO_BASE = 10000;//祭祀概率基数
	
	
	/**
	 * 祭祀可以获得的道具列表
	 * 
	 * @param itemLevel
	 * @param itemId
	 * @return
	 */
	public static List<Integer> canGrantItems(int itemLevel,int itemId) {
		Fete fete = getFete(itemId);
		List<Integer> canAddItemList = new ArrayList<Integer>();
		String canAddItems = fete.getItemPro();
		if (canAddItems != null && canAddItems.length() > 0) {
			String[] cells = canAddItems.split("[|]");
			if (cells != null) {
				for (String cell : cells) {
					if (cell != null && cell.indexOf(":") != -1) {
						String[] n = cell.split("[:]");
						Integer id = Integer.parseInt(n[0]);
						if (id != null) {
							canAddItemList.add(id.intValue());
						}
					}
				}
			}
		}
		return canAddItemList;
	}
	
	
	/**
	 * 祭祀获得道具，根据概率随机（道具等级/5）次，次数为0的时候随机1次，基数为10000
	 * 
	 * @param itemLevel
	 * @param itemId
	 * @return 返回随机好的道具及其数量，格式itemId:num|itemId:num
	 */
	public static String doGrantItems(int itemLevel,int itemId) {
		int times = 1;
		if (itemLevel >= 2 * ITEM_INTERVAL_LEVEL) {
			times = itemLevel / ITEM_INTERVAL_LEVEL;
		}
		Fete fete = getFete(itemId);
		String randomItems = "";
		if (fete != null) {
			randomItems = Utils.calculateProbability(fete.getItemPro(), PRO_BASE, times);
		}
		return randomItems;
	}
	
	/**
	 * 祭祀经验解析
	 * 
	 * @param itemLevel
	 * @param itemId
	 * @return
	 */
	public static int doGrandExp(int itemLevel,int itemId) {
		int multiple = 1;
		if (itemLevel >= 2 * COMMON_INTERVAL_LEVEL) {
			multiple = itemLevel / COMMON_INTERVAL_LEVEL;
		}
		Fete fete = getFete(itemId);
		int exp = 0;
		if (fete != null) {
			exp = multiple * fete.getExp();
		}
		return exp;
	}
	
	/**
	 * 祭祀碎银解析
	 * 
	 * @param itemLevel
	 * @param itemId
	 * @return
	 */
	public static int doGrandMoney(int itemLevel,int itemId) {
		int multiple = 1;
		if (itemLevel >= 2 * COMMON_INTERVAL_LEVEL) {
			multiple = itemLevel / COMMON_INTERVAL_LEVEL;
		}
		Fete fete = getFete(itemId);
		int money = 0;
		if (fete != null) {
			money = multiple * fete.getMoney();
		}
		return money;
	}
	
	/**
	 * 祭祀金币解析
	 * 
	 * @param itemId
	 * @return
	 */
	public static int doGrandGold(int itemId) {
		Fete fete = getFete(itemId);
		int gold = 0;
		if (fete != null) {
			String value = fete.getGoldPro();
			String[] cell = value.split("[|]");
			for (String c: cell) {
				if (c != null && c.indexOf(":") != -1) {
					String[] d = c.split(":");
					int luck = new Random().nextInt(PRO_BASE);
					if (luck < Integer.parseInt(d[1])) {
						gold = Integer.parseInt(d[0]);
					}
				}
			}
		}
		return gold;
	}
	
	/**根据类型获得祭祀结果
	 * @param itemId
	 * @return
	 */
	public static final Fete getFete(int itemId) {
		Fete fete = FeteMG.getItem2Fete().get(itemId);
		if (fete == null) {
			Item item = ItemMG.instance.getItem(itemId);
			if (item != null) {
				fete = FeteMG.getType2Fete().get(item.getType());
			}
		}
		return fete;
	}
}
